package com.ezops.poc.validations.result;

public class ValidationResult implements IValidationResult {

	private FieldValidationError validationError;

	private boolean isValid;

	public static ValidationResult getValidResponse() {
		return new ValidationResult();
	}

	public static ValidationResult getErrorResponse(FieldValidationError error) {
		return new ValidationResult(error);
	}

	private ValidationResult() {
		isValid = true;
	}

	private ValidationResult(FieldValidationError error) {
		if (error != null) {
			this.validationError = error;
			this.isValid = false;
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ezops.poc.validations.validators.IValidationResult#isValid()
	 */
	public boolean isValid() {
		return isValid;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ezops.poc.validations.validators.IValidationResult#setError(com.ezops
	 * .poc.validations.FieldValidationError)
	 */
	public void setError(FieldValidationError error) {
		if (error != null) {
			this.validationError = error;
			this.isValid = false;
		}
	}

	public static ValidationResult getErrorResponse(final String field,
			final String error) {
		return new ValidationResult(new FieldValidationError() {

			@Override
			public String getFieldName() {
				return field;
			}

			@Override
			public String getErrorMessage() {
				return error;
			}
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ezops.poc.validations.validators.IValidationResult#getValidationError
	 * ()
	 */
	public FieldValidationError getValidationError() {
		return validationError;
	}
}
